package com.wulian.controller;

import com.wulian.entities.Doctor;
import com.wulian.entities.Patient;
import com.wulian.result.CodeStatus;
import com.wulian.result.ResponseResult;
import com.wulian.service.DoctorService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "医生操作接口")
@RestController
@RequestMapping(value = "/doctor")
public class DoctorController {

    @Resource
    DoctorService doctorService;

    @ApiOperation("通过id查医生")
    @GetMapping(value = "/{doctor_id}")
    public ResponseResult<Doctor> selDocById(@PathVariable Long doctor_id) {
        if (doctor_id < 0) {
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST, "参数错误");
        }
        try {
            Doctor doctor = doctorService.selDocById(doctor_id);
            if (doctor != null) {
                return new ResponseResult<>(CodeStatus.OK, "查询成功", doctor);
            } else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查到相关数据");
            }
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }

    }

    @ApiOperation("查询所有医生")
    @GetMapping(value = "/")
    public ResponseResult<List<Doctor>> selAllDoctor() {
        try {
            List<Doctor> doctorList = doctorService.selAllDoctor();
            if (doctorList != null) {
                return new ResponseResult<>(CodeStatus.OK, "查询成功", doctorList);
            } else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查到相关数据");
            }
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }


    @ApiOperation("医生登录")
    @PostMapping(value = "/login")
    public ResponseResult<Object> registerDoctor(@RequestBody Doctor doctor) {
        try {
            Integer doctor_id = doctorService.loginDoctor(doctor);
            if (doctor_id == null) {
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "密码或账号错误");
            } else {
                return new ResponseResult<>(CodeStatus.OK, "登录成功", doctor_id);
            }
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }

    @ApiOperation("添加医生")
    @PostMapping(value = "/add")
    public ResponseResult<Object> addDoctor(@RequestBody Doctor doctor) {
        try {
            doctorService.addDoctor(doctor);
            return new ResponseResult<>(CodeStatus.OK, "添加成功");
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }

    }

    @ApiOperation("修改医生信息")
    @PutMapping(value = "/")
    public ResponseResult<Object> UpdateDoctor(@RequestBody Doctor doctor) {
        try {
            doctorService.updateDoctor(doctor);
            return new ResponseResult<>(CodeStatus.OK, "修改成功");
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }

    @ApiOperation("通过医生名查病人")
    @GetMapping(value = "/name/{name}")//
    public ResponseResult<List<Patient>> getCountByDocName(@PathVariable String name) {
        try {
            List<Patient> patientList = doctorService.getPatientByDocName(name);
            //patientList.size();病人总数
            if (patientList != null) {

                return new ResponseResult<>(CodeStatus.OK, "查询成功", patientList);
            } else {
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查到相关数据");
            }
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }

    @ApiOperation("分页查询医生信息")
    @GetMapping("/{pageNum}/{size}")
    public ResponseResult<List<Doctor>> getDoctorsByPage(@PathVariable Integer pageNum, @PathVariable Integer size) {
        if (pageNum <= 0 && size <= 0) {
            return new ResponseResult<>(CodeStatus.ILLEGAL_REQUEST, "参数错误");
        }
        List<Doctor> doctorList = doctorService.getAllByPage(pageNum, size);
        try {
            if (doctorList == null) {
                return new ResponseResult<>(CodeStatus.NOT_FOUND, "未查询到相关数据");
            } else {
                return new ResponseResult<>(CodeStatus.OK, "查询成功", doctorList);
            }
        } catch (Exception e) {
            return new ResponseResult<>(CodeStatus.FAIL, e);
        }
    }
}
